package com.ustc.emailUI;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.JFrame;

import com.ustc.emailcheck.Account;

public class ManagementAccountUI extends JFrame implements ActionListener {

	private JFrame manageAccountJframe;
	private Box boxV1, boxV2;
	private CheckboxGroup allEmails;
	private Button newAccountButton, editAccountButton, deleteAccountButton;
	private Container contentpane;

	public ManagementAccountUI() {
		// TODO Auto-generated constructor stub
		super();
	}

	/**
	 * @param args
	 */
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		new ManagementAccountUI().showManageAccountUI();
//
//	}

	public void showManageAccountUI() {
		manageAccountJframe = new JFrame("Account Management");
		manageAccountJframe.setVisible(true);
		manageAccountJframe.setBounds(250, 250, 250, 200);
		manageAccountJframe.setBackground(Color.gray);
		manageAccountJframe.setAlwaysOnTop(true);
		manageAccountJframe.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				manageAccountJframe.setVisible(false);
			}
		});

		boxV1 = Box.createVerticalBox();
		boxV1.add(new Label("All accounts have been registered:"));
		try {
			allEmails = new CheckboxGroup();
			String emailAccount = null;
			Account[] reAccounts = Account.loadAccount();
			
			for (int i = 0; i < reAccounts.length; i++) {
				emailAccount = reAccounts[i].getUsername();
				boxV1.add(new Checkbox(emailAccount, false, allEmails));
				boxV1.add(Box.createVerticalStrut(8));
			}

		} catch (NullPointerException nullexception) {
			// TODO: handle exception
			nullexception.printStackTrace();
			 manageAccountJframe.setVisible(false);
             new ExceptionNotifyUI().showExceptionNotifyUI("Please create an account first");
             new ConfigureInfoUI().showConfigureUI();

		}

		boxV2 = Box.createVerticalBox();
		boxV2.add(Box.createVerticalStrut(20));
		newAccountButton = new Button("new Account");
		newAccountButton.addActionListener(this);
		newAccountButton.setPreferredSize(new Dimension(80,20));
		newAccountButton.setMaximumSize(new Dimension(80,20));
		newAccountButton.setMinimumSize(new Dimension(80,20));
		boxV2.add(newAccountButton);
		boxV2.add(Box.createVerticalStrut(8));

		editAccountButton = new Button("edit");
		editAccountButton.addActionListener(this);
		editAccountButton.setPreferredSize(new Dimension(80,20));
		editAccountButton.setMaximumSize(new Dimension(80,20));
		editAccountButton.setMinimumSize(new Dimension(80,20));
		boxV2.add(editAccountButton);
		boxV2.add(Box.createVerticalStrut(8));

		deleteAccountButton = new Button("delete");
		deleteAccountButton.addActionListener(this);
		deleteAccountButton.setPreferredSize(new Dimension(80,20));
		deleteAccountButton.setMaximumSize(new Dimension(80,20));
		deleteAccountButton.setMinimumSize(new Dimension(80,20));
		boxV2.add(deleteAccountButton);
		boxV2.add(Box.createVerticalStrut(8));

		contentpane = manageAccountJframe.getContentPane();
		contentpane.add(boxV1, BorderLayout.WEST);
		contentpane.add(Box.createVerticalStrut(10), BorderLayout.CENTER);
		contentpane.add(boxV2, BorderLayout.EAST);
		manageAccountJframe.pack();

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == newAccountButton) {
			manageAccountJframe.setVisible(false);
			ConfigureInfoUI configUI=new ConfigureInfoUI();
			configUI.showConfigureUI();
		}
		if (e.getSource() == editAccountButton) {

			try {
				String selectedEmail = allEmails.getSelectedCheckbox()
						.getLabel();

				if (selectedEmail != null) {
					ConfigureInfoUI configUI=new ConfigureInfoUI();
					Account[] accounts = Account.loadAccount();
					Account accountEdit = new Account();
					Account[] newAccounts = new Account[accounts.length-1];
					for (int i = 0,j=0; i < accounts.length; i++,j++) {
						
						if (selectedEmail.equals(accounts[i].getUsername())) {

							accountEdit = accounts[i];
							configUI.showConfigureUI();
							configUI.getNikName().setText(accountEdit.getName());
							configUI.getServerTypeChoice().select(accountEdit.getServerType());
							configUI.getServer().setText(accountEdit.getServer());
							configUI.getAccountName().setText(accountEdit.getUsername());
							configUI.getPassword().setText(accountEdit.getPassword());
							configUI.getViewTool().setText(accountEdit.getEmailclient());
							
							j--;
							
						}
						else{
							newAccounts[j] = accounts[i];
						}
					}
					Account.saveAccount(newAccounts);
					manageAccountJframe.setVisible(false);
				}
			} catch (NullPointerException nullpointer) {
				// TODO: handle exception
				ExceptionNotifyUI exceptionNotifyUI=new ExceptionNotifyUI();
				exceptionNotifyUI.showExceptionNotifyUI("Please choose one account to edit!");
			}

		}
		if (e.getSource() == deleteAccountButton) {
			try {
				String selectedEmail = allEmails.getSelectedCheckbox()
						.getLabel();

				if (selectedEmail != null) {
					Account[] accounts = Account.loadAccount();
					Account[] newAccounts = new Account[accounts.length-1];
					for (int i = 0, j = 0; i < accounts.length; i++, j++) {
						if (selectedEmail.equals(accounts[i].getUsername())) {
							j--;
						} else {
							newAccounts[j] = accounts[i];

						}
					}
					Account.saveAccount(newAccounts);
					manageAccountJframe.setVisible(false);
					new ManagementAccountUI().showManageAccountUI();
				}

			} catch (NullPointerException nullpointer) {
				// TODO: handle exception
				ExceptionNotifyUI exceptionNotifyUI=new ExceptionNotifyUI();
				exceptionNotifyUI.showExceptionNotifyUI("Please choose one account to edit!");
			}

		}
		else{
			
		}
	}

}
